//
//  Fraction.m
//  02-ClassAndObject
//
//  Created by dllo on 16/7/5.
//  Copyright © 2016年 daming. All rights reserved.
//

#import "Fraction.h"

@implementation Fraction
//自定义初始化
- (instancetype)initWithNumerator:(NSInteger)numerator
                      denominator:(NSInteger)denominator {
    
    self = [super init];
    if (self) {
        _denominator = denominator;
        _numerator = numerator;
    }
    return self;
}
// 便利构造器
+ (instancetype)fractionNumerator:(NSInteger)numerator
                      denominator:(NSInteger)denominator {
    
    return [[Fraction alloc] initWithNumerator:numerator denominator:denominator];
    
}
// 加减乘除
- (void)addOfFraction:(Fraction *)frac {
    
    NSInteger newNum = self.numerator * frac.denominator + self.denominator * frac.numerator;
    NSInteger newDen = self.denominator * frac.denominator;
    Fraction *newFraction = [Fraction fractionNumerator:newNum denominator:newDen];
    [newFraction reductionOfFraction];
    if (newFraction.denominator == 0) {
        NSLog(@"分母不能为零");
    } else if (newDen == 0) {
        NSLog(@"两分数相加 = 0");
    } else {
        if (newNum % newDen == 0) {
        NSLog(@"两分数相加 = %ld", newFraction.numerator);
        } else {
        NSLog(@"两分数相加 = %ld/%ld", newFraction.numerator, newFraction.denominator);
        }
    }
}

- (void)subtractOfFraction:(Fraction *)frac {
    NSInteger newNum = self.numerator * frac.denominator - self.denominator * frac.numerator;
    NSInteger newDen = self.denominator * frac.denominator;
    Fraction *newFraction = [Fraction fractionNumerator:newNum denominator:newDen];
    [newFraction reductionOfFraction];
    if (newFraction.denominator == 0) {
        NSLog(@"分母不能为零");
    } else if (newNum == 0) {
        NSLog(@"两分数相减 = 0");
    } else {
        if (newNum % newDen == 0) {
            NSLog(@"两分数相减 = %ld", newFraction.numerator);
        } else {
            NSLog(@"两分数相减 = %ld/%ld", newFraction.numerator, newFraction.denominator);
        }
    }
}

- (void)multiplyOfFraction:(Fraction *)frac {
    NSInteger newNum = self.numerator * frac.numerator;
    NSInteger newDen = self.denominator * frac.denominator;
    Fraction *newFraction = [Fraction fractionNumerator:newNum denominator:newDen];
    [newFraction reductionOfFraction];
    if (newFraction.denominator == 0) {
        NSLog(@"分母不能为零");
    } else if (newNum == 0){
        NSLog(@"两分数相乘 = 0");
    } else {
        if (newNum % newDen == 0) {
            NSLog(@"两分数相乘 = %ld", newFraction.numerator);
        } else {
            NSLog(@"两分数相乘 = %ld/%ld", newFraction.numerator, newFraction.denominator);
        }
    }
}

- (void)divideOfFraction:(Fraction *)frac {
    NSInteger newNum = self.numerator * frac.denominator;
    NSInteger newDen = self.denominator *frac.numerator;
    Fraction *newFraction = [Fraction fractionNumerator:newNum denominator:newDen];
    [newFraction reductionOfFraction];
    if (newFraction.denominator == 0) {
        NSLog(@"分母不能为零");
    } else if (newNum == newDen) {
        NSLog(@"两分数相除 = 1");
    } else {
        if (newNum % newDen == 0) {
            NSLog(@"两分数相除 = %ld", newFraction.numerator);
        } else {
            NSLog(@"两分数相除 = %ld/%ld", newFraction.numerator, newFraction.denominator);
        }
    }
}

/** 最大公约数 */
- (NSInteger)greatestCommonMeasureOfNumerator:(NSInteger)numerator
                                  denominator:(NSInteger)denominator {
   
    NSInteger  temp = 0;
    if (numerator < 0) {
        numerator = -numerator;
    }
    while (denominator != 0) {
        temp = numerator % denominator;
        numerator = denominator;
        denominator = temp;
    }
    return numerator;
}

/** 约分 */
- (void)reductionOfFraction{
    
    NSInteger maxDivisor = [self greatestCommonMeasureOfNumerator:_numerator denominator:_denominator];
    self.numerator /= maxDivisor;
    self.denominator /= maxDivisor;
}

/** 比较大小 */
- (void)compareFraction:(Fraction *)frac {
    
    NSInteger newNum = self.numerator * frac.denominator - self.denominator * frac.numerator;
    NSInteger newDen = self.denominator * frac.denominator;
    if (newDen != 0) {
        if (newNum > 0) {
            NSLog(@"两分数最大的是%ld/%ld, 两分数最小的是%ld/%ld", self.numerator, self.denominator, frac.numerator, frac.denominator);
        } else if (newNum == 0) {
            NSLog(@"两个分数相等");
        } else {
            NSLog(@"两分数最大的是%ld/%ld, 两分数最小的是%ld/%ld", frac.numerator, frac.denominator, self.numerator, self.denominator);
        }
    } else {
        NSLog(@"分母不能为零");
    }
}

@end
